%%-*- mode: erlang -*-
%% emq_auth_mysql config mapping
{mapping, "auth.mysql.server", "emq_auth_mysql.server", [
  {default, {"127.0.0.1", 3306}},
  {datatype, [integer, ip, string]}
]}.

{mapping, "auth.mysql.pool", "emq_auth_mysql.server", [
  {default, 8},
  {datatype, integer}
]}.

{mapping, "auth.mysql.username", "emq_auth_mysql.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "auth.mysql.password", "emq_auth_mysql.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "auth.mysql.database", "emq_auth_mysql.server", [
  {default, "mqtt"},
  {datatype, string}
]}.

{translation, "emq_auth_mysql.server", fun(Conf) ->
  {MyHost, MyPort} =
  case cuttlefish:conf_get("auth.mysql.server", Conf) of
    {Ip, Port} -> {Ip, Port};
    S          -> case string:tokens(S, ":") of
                    [Domain]       -> {Domain, 3306};
                    [Domain, Port] -> {Domain, list_to_integer(Port)}
                  end
    end,
  Pool = cuttlefish:conf_get("auth.mysql.pool", Conf),
  Username = cuttlefish:conf_get("auth.mysql.username", Conf),
  Passwd = cuttlefish:conf_get("auth.mysql.password", Conf),
  DB = cuttlefish:conf_get("auth.mysql.database", Conf),
  [{pool_size, Pool},
   {auto_reconnect, 1},
   {host, MyHost},
   {port, MyPort},
   {user, Username},
   {password, Passwd},
   {database, DB},
   {encoding, utf8},
   {keep_alive, true}]
end}.

{mapping, "auth.mysql.auth_query", "emq_auth_mysql.auth_query", [
  {datatype, string}
]}.

{mapping, "auth.mysql.password_hash", "emq_auth_mysql.password_hash", [
  {datatype, string}
]}.

{mapping, "auth.mysql.super_query", "emq_auth_mysql.super_query", [
  {datatype, string}
]}.

{mapping, "auth.mysql.acl_query", "emq_auth_mysql.acl_query", [
  {datatype, string}
]}.

{mapping, "auth.mysql.acl_nomatch", "emq_auth_mysql.acl_nomatch", [
  {default, deny},
  {datatype, {enum, [deny, allow]}}
]}.

{translation, "emq_auth_mysql.password_hash", fun(Conf) ->
  HashValue = cuttlefish:conf_get("auth.mysql.password_hash", Conf),
    case string:tokens(HashValue, " ") of
        [Hash]           -> list_to_atom(Hash);
        [Prefix, Suffix] -> {list_to_atom(Prefix), list_to_atom(Suffix)};
        [Hash, MacFun, Iterations, Dklen] -> {list_to_atom(Hash), list_to_atom(MacFun), list_to_integer(Iterations), list_to_integer(Dklen)};
        _                -> plain
    end
end}.
